package com.zjw.zy.coreui.fragment.ocr

import android.content.Intent
import android.os.Bundle
import android.view.View
import com.baidu.ocr.sdk.OCR
import com.baidu.ocr.sdk.OnResultListener
import com.baidu.ocr.sdk.exception.OCRError
import com.baidu.ocr.sdk.model.AccessToken
import com.zjw.zy.R
import com.zjw.zy.base.BaseFragment
import com.zjw.zy.constant.AppConstant
import com.zjw.zy.ext.longToast
import com.zjw.zy.ext.mainActivity
import com.zjw.zy.utils.AppUtils
import com.zjw.zy.utils.OcrUtils
import com.zjw.zy.utils.PathUtils
import com.zjw.zy.utils.WindowUtils
import ocr.ocr.ui.camera.CameraActivity

/**
 * @author     ：zhong.jw
 * @date       ：Created in 2022/10/19 15:15
 * @description：
 */
class OcrFragment : BaseFragment() {

    companion object {


        const val REQUEST_CODE_GENERAL = 105
        const val REQUEST_CODE_GENERAL_BASIC = 106
        const val REQUEST_CODE_ACCURATE_BASIC = 107
        const val REQUEST_CODE_ACCURATE = 108
        const val REQUEST_CODE_GENERAL_ENHANCED = 109
        const val REQUEST_CODE_GENERAL_WEBIMAGE = 110
        const val REQUEST_CODE_VEHICLE_LICENSE = 120
        const val REQUEST_CODE_DRIVING_LICENSE = 121
        const val REQUEST_CODE_LICENSE_PLATE = 122
        const val REQUEST_CODE_BUSINESS_LICENSE = 123
        const val REQUEST_CODE_RECEIPT = 124

        const val REQUEST_CODE_PASSPORT = 125 // 护照识别

        const val REQUEST_CODE_NUMBERS = 126
        const val REQUEST_CODE_QRCODE = 127 // 二维码识别

        const val REQUEST_CODE_BUSINESSCARD = 128
        const val REQUEST_CODE_HANDWRITING = 129
        const val REQUEST_CODE_LOTTERY = 130
        const val REQUEST_CODE_VATINVOICE = 131
        const val REQUEST_CODE_CUSTOM = 132
        const val REQUEST_CODE_TAXIRECEIPT = 133
        const val REQUEST_CODE_VINCODE = 134
        const val REQUEST_CODE_TRAINTICKET = 135
        const val REQUEST_CODE_TRIP_TICKET = 136 // 行程单

        const val REQUEST_CODE_CAR_SELL_INVOICE = 137 // 机动车销售发票

        const val REQUEST_CODE_VIHICLE_SERTIFICATION = 138 // 车辆合格证

        const val REQUEST_CODE_EXAMPLE_DOC_REG = 139 // 试卷分析和识别

        const val REQUEST_CODE_WRITTEN_TEXT = 140 // 手写文字识别

        const val REQUEST_CODE_HUKOU_PAGE = 141 // 户口本识别

        const val REQUEST_CODE_NORMAL_MACHINE_INVOICE = 142 // 普通机打发票识别

        const val REQUEST_CODE_WEIGHT_NOTE = 143 //磅单识别

        const val REQUEST_CODE_MEDICAL_DETAIL = 144 //医疗费用明细识别

        const val REQUEST_CODE_ONLINE_TAXI_ITINERARY = 145 //网约车行程单识别
    }

    override fun onPageCreate() {
        super.onPageCreate()
        if (!OcrUtils.isAccess()) {
            OcrUtils.init(AppUtils.getApp())
        }
    }

    override fun onPageEnter() {
        WindowUtils.TopBar.showBack()
        WindowUtils.FootBar.hide()
        WindowUtils.TopBar.hideAllRightButton()
    }

    override fun onViewRestore(savedInstanceState: Bundle?) {

    }

    override fun onSubscribe() {

    }

    override fun onBindViewListener() {
        // 通用文字识别
        findViewById<View>(R.id.general_basic_button).setOnClickListener(View.OnClickListener {
            if (!checkTokenStatus()) {
                return@OnClickListener
            }
            val intent = Intent(mainActivity(), CameraActivity::class.java)
            intent.putExtra(
                CameraActivity.KEY_OUTPUT_FILE_PATH,
                PathUtils.genericOrcImageFilePath()
            )
            intent.putExtra(
                CameraActivity.KEY_CONTENT_TYPE,
                CameraActivity.CONTENT_TYPE_GENERAL
            )
            startActivityForResult(intent, REQUEST_CODE_GENERAL_BASIC)
        })

        // 通用文字识别(高精度版)

        // 通用文字识别(高精度版)
        findViewById<View>(R.id.accurate_basic_button).setOnClickListener(View.OnClickListener {
            if (!checkTokenStatus()) {
                return@OnClickListener
            }
            val intent = Intent(mainActivity(), CameraActivity::class.java)
            intent.putExtra(
                CameraActivity.KEY_OUTPUT_FILE_PATH,
                PathUtils.genericOrcImageFilePath()
            )
            intent.putExtra(
                CameraActivity.KEY_CONTENT_TYPE,
                CameraActivity.CONTENT_TYPE_GENERAL
            )
            startActivityForResult(intent, REQUEST_CODE_ACCURATE_BASIC)
        })

        // 通用文字识别（含位置信息版）

        // 通用文字识别（含位置信息版）
        findViewById<View>(R.id.general_button).setOnClickListener(View.OnClickListener {
            if (!checkTokenStatus()) {
                return@OnClickListener
            }
            val intent = Intent(mainActivity(), CameraActivity::class.java)
            intent.putExtra(
                CameraActivity.KEY_OUTPUT_FILE_PATH,
                PathUtils.genericOrcImageFilePath()
            )
            intent.putExtra(
                CameraActivity.KEY_CONTENT_TYPE,
                CameraActivity.CONTENT_TYPE_GENERAL
            )
            startActivityForResult(intent, REQUEST_CODE_GENERAL)
        })

        // 通用文字识别（含位置信息高精度版）

        // 通用文字识别（含位置信息高精度版）
        findViewById<View>(R.id.accurate_button).setOnClickListener(View.OnClickListener {
            if (!checkTokenStatus()) {
                return@OnClickListener
            }
            val intent = Intent(mainActivity(), CameraActivity::class.java)
            intent.putExtra(
                CameraActivity.KEY_OUTPUT_FILE_PATH,
                PathUtils.genericOrcImageFilePath()
            )
            intent.putExtra(
                CameraActivity.KEY_CONTENT_TYPE,
                CameraActivity.CONTENT_TYPE_GENERAL
            )
            startActivityForResult(intent, REQUEST_CODE_ACCURATE)
        })

        // 通用文字识别（含生僻字版）

        // 通用文字识别（含生僻字版）
        findViewById<View>(R.id.general_enhance_button).setOnClickListener(View.OnClickListener {
            if (!checkTokenStatus()) {
                return@OnClickListener
            }
            val intent = Intent(mainActivity(), CameraActivity::class.java)
            intent.putExtra(
                CameraActivity.KEY_OUTPUT_FILE_PATH,
                PathUtils.genericOrcImageFilePath()
            )
            intent.putExtra(
                CameraActivity.KEY_CONTENT_TYPE,
                CameraActivity.CONTENT_TYPE_GENERAL
            )
            startActivityForResult(intent, REQUEST_CODE_GENERAL_ENHANCED)
        })

        // 网络图片识别

        // 网络图片识别
        findViewById<View>(R.id.general_webimage_button).setOnClickListener(View.OnClickListener {
            if (!checkTokenStatus()) {
                return@OnClickListener
            }
            val intent = Intent(mainActivity(), CameraActivity::class.java)
            intent.putExtra(
                CameraActivity.KEY_OUTPUT_FILE_PATH,
                PathUtils.genericOrcImageFilePath()
            )
            intent.putExtra(
                CameraActivity.KEY_CONTENT_TYPE,
                CameraActivity.CONTENT_TYPE_GENERAL
            )
            startActivityForResult(intent, REQUEST_CODE_GENERAL_WEBIMAGE)
        })

        // 身份证识别

        // 身份证识别
        findViewById<View>(R.id.idcard_button).setOnClickListener(View.OnClickListener {
            if (!checkTokenStatus()) {
                return@OnClickListener
            }
//            val intent = Intent(mainActivity(), IDCardActivity::class.java)
//            startActivity(intent)
        })

        // 银行卡识别

        // 银行卡识别
        findViewById<View>(R.id.bankcard_button).setOnClickListener(View.OnClickListener {
            if (!checkTokenStatus()) {
                return@OnClickListener
            }
//            val intent = Intent(mainActivity(), BankCardActivity::class.java)
//            startActivity(intent)
        })

        // 行驶证识别

        // 行驶证识别
        findViewById<View>(R.id.vehicle_license_button).setOnClickListener(View.OnClickListener {
            if (!checkTokenStatus()) {
                return@OnClickListener
            }
            val intent = Intent(mainActivity(), CameraActivity::class.java)
            intent.putExtra(
                CameraActivity.KEY_OUTPUT_FILE_PATH,
                PathUtils.genericOrcImageFilePath()
            )
            intent.putExtra(
                CameraActivity.KEY_CONTENT_TYPE,
                CameraActivity.CONTENT_TYPE_GENERAL
            )
            startActivityForResult(intent, REQUEST_CODE_VEHICLE_LICENSE)
        })

        // 驾驶证识别

        // 驾驶证识别
        findViewById<View>(R.id.driving_license_button).setOnClickListener(View.OnClickListener {
            if (!checkTokenStatus()) {
                return@OnClickListener
            }
            val intent = Intent(mainActivity(), CameraActivity::class.java)
            intent.putExtra(
                CameraActivity.KEY_OUTPUT_FILE_PATH,
                PathUtils.genericOrcImageFilePath()
            )
            intent.putExtra(
                CameraActivity.KEY_CONTENT_TYPE,
                CameraActivity.CONTENT_TYPE_GENERAL
            )
            startActivityForResult(intent, REQUEST_CODE_DRIVING_LICENSE)
        })

        // 车牌识别

        // 车牌识别
        findViewById<View>(R.id.license_plate_button).setOnClickListener(View.OnClickListener {
            if (!checkTokenStatus()) {
                return@OnClickListener
            }
            val intent = Intent(mainActivity(), CameraActivity::class.java)
            intent.putExtra(
                CameraActivity.KEY_OUTPUT_FILE_PATH,
                PathUtils.genericOrcImageFilePath()
            )
            intent.putExtra(
                CameraActivity.KEY_CONTENT_TYPE,
                CameraActivity.CONTENT_TYPE_GENERAL
            )
            startActivityForResult(intent, REQUEST_CODE_LICENSE_PLATE)
        })

        // 营业执照识别

        // 营业执照识别
        findViewById<View>(R.id.business_license_button).setOnClickListener(View.OnClickListener {
            if (!checkTokenStatus()) {
                return@OnClickListener
            }
            val intent = Intent(mainActivity(), CameraActivity::class.java)
            intent.putExtra(
                CameraActivity.KEY_OUTPUT_FILE_PATH,
                PathUtils.genericOrcImageFilePath()
            )
            intent.putExtra(
                CameraActivity.KEY_CONTENT_TYPE,
                CameraActivity.CONTENT_TYPE_GENERAL
            )
            startActivityForResult(intent, REQUEST_CODE_BUSINESS_LICENSE)
        })

        // 通用票据识别

        // 通用票据识别
        findViewById<View>(R.id.receipt_button).setOnClickListener(View.OnClickListener {
            if (!checkTokenStatus()) {
                return@OnClickListener
            }
            val intent = Intent(mainActivity(), CameraActivity::class.java)
            intent.putExtra(
                CameraActivity.KEY_OUTPUT_FILE_PATH,
                PathUtils.genericOrcImageFilePath()
            )
            intent.putExtra(
                CameraActivity.KEY_CONTENT_TYPE,
                CameraActivity.CONTENT_TYPE_GENERAL
            )
            startActivityForResult(intent, REQUEST_CODE_RECEIPT)
        })

        // 增值税发票识别

        // 增值税发票识别
        findViewById<View>(R.id.value_added_button).setOnClickListener(View.OnClickListener {
            if (!checkTokenStatus()) {
                return@OnClickListener
            }
            val intent = Intent(mainActivity(), CameraActivity::class.java)
            intent.putExtra(
                CameraActivity.KEY_OUTPUT_FILE_PATH,
                PathUtils.genericOrcImageFilePath()
            )
            intent.putExtra(
                CameraActivity.KEY_CONTENT_TYPE,
                CameraActivity.CONTENT_TYPE_GENERAL
            )
            startActivityForResult(intent, REQUEST_CODE_VATINVOICE)
        })

        // 出租车票

        // 出租车票
        findViewById<View>(R.id.taxi_coupon_button).setOnClickListener(View.OnClickListener {
            if (!checkTokenStatus()) {
                return@OnClickListener
            }
            val intent = Intent(mainActivity(), CameraActivity::class.java)
            intent.putExtra(
                CameraActivity.KEY_OUTPUT_FILE_PATH,
                PathUtils.genericOrcImageFilePath()
            )
            intent.putExtra(
                CameraActivity.KEY_CONTENT_TYPE,
                CameraActivity.CONTENT_TYPE_GENERAL
            )
            startActivityForResult(intent, REQUEST_CODE_TAXIRECEIPT)
        })

        // VIN码

        // VIN码
        findViewById<View>(R.id.vin_button).setOnClickListener(View.OnClickListener {
            if (!checkTokenStatus()) {
                return@OnClickListener
            }
            val intent = Intent(mainActivity(), CameraActivity::class.java)
            intent.putExtra(
                CameraActivity.KEY_OUTPUT_FILE_PATH,
                PathUtils.genericOrcImageFilePath()
            )
            intent.putExtra(
                CameraActivity.KEY_CONTENT_TYPE,
                CameraActivity.CONTENT_TYPE_GENERAL
            )
            startActivityForResult(intent, REQUEST_CODE_VINCODE)
        })

        // 火车票

        // 火车票
        findViewById<View>(R.id.train_ticket_button).setOnClickListener(View.OnClickListener {
            if (!checkTokenStatus()) {
                return@OnClickListener
            }
            val intent = Intent(mainActivity(), CameraActivity::class.java)
            intent.putExtra(
                CameraActivity.KEY_OUTPUT_FILE_PATH,
                PathUtils.genericOrcImageFilePath()
            )
            intent.putExtra(
                CameraActivity.KEY_CONTENT_TYPE,
                CameraActivity.CONTENT_TYPE_GENERAL
            )
            startActivityForResult(intent, REQUEST_CODE_TRAINTICKET)
        })

        // 数字识别

        // 数字识别
        findViewById<View>(R.id.number_button).setOnClickListener(View.OnClickListener {
            if (!checkTokenStatus()) {
                return@OnClickListener
            }
            val intent = Intent(mainActivity(), CameraActivity::class.java)
            intent.putExtra(
                CameraActivity.KEY_OUTPUT_FILE_PATH,
                PathUtils.genericOrcImageFilePath()
            )
            intent.putExtra(
                CameraActivity.KEY_CONTENT_TYPE,
                CameraActivity.CONTENT_TYPE_GENERAL
            )
            startActivityForResult(intent, REQUEST_CODE_NUMBERS)
        })
        // 二维码识别
        // 二维码识别
        findViewById<View>(R.id.qrcode_recognize_button).setOnClickListener(View.OnClickListener {
            if (!checkTokenStatus()) {
                return@OnClickListener
            }
            val intent = Intent(mainActivity(), CameraActivity::class.java)
            intent.putExtra(
                CameraActivity.KEY_OUTPUT_FILE_PATH,
                PathUtils.genericOrcImageFilePath()
            )
            intent.putExtra(
                CameraActivity.KEY_CONTENT_TYPE,
                CameraActivity.CONTENT_TYPE_GENERAL
            )
            startActivityForResult(intent, REQUEST_CODE_QRCODE)
        })

        // 行程单识别

        // 行程单识别
        findViewById<View>(R.id.trip_ticket_button).setOnClickListener(View.OnClickListener {
            if (!checkTokenStatus()) {
                return@OnClickListener
            }
            val intent = Intent(mainActivity(), CameraActivity::class.java)
            intent.putExtra(
                CameraActivity.KEY_OUTPUT_FILE_PATH,
                PathUtils.genericOrcImageFilePath()
            )
            intent.putExtra(
                CameraActivity.KEY_CONTENT_TYPE,
                CameraActivity.CONTENT_TYPE_GENERAL
            )
            startActivityForResult(intent, REQUEST_CODE_TRIP_TICKET)
        })

        // 机动车销售发票识别

        // 机动车销售发票识别
        findViewById<View>(R.id.car_sell_ticket_button).setOnClickListener(View.OnClickListener {
            if (!checkTokenStatus()) {
                return@OnClickListener
            }
            val intent = Intent(mainActivity(), CameraActivity::class.java)
            intent.putExtra(
                CameraActivity.KEY_OUTPUT_FILE_PATH,
                PathUtils.genericOrcImageFilePath()
            )
            intent.putExtra(
                CameraActivity.KEY_CONTENT_TYPE,
                CameraActivity.CONTENT_TYPE_GENERAL
            )
            startActivityForResult(intent, REQUEST_CODE_CAR_SELL_INVOICE)
        })

        // 车辆合格证

        // 车辆合格证
        findViewById<View>(R.id.vehicle_certificate_button).setOnClickListener(View.OnClickListener {
            if (!checkTokenStatus()) {
                return@OnClickListener
            }
            val intent = Intent(mainActivity(), CameraActivity::class.java)
            intent.putExtra(
                CameraActivity.KEY_OUTPUT_FILE_PATH,
                PathUtils.genericOrcImageFilePath()
            )
            intent.putExtra(
                CameraActivity.KEY_CONTENT_TYPE,
                CameraActivity.CONTENT_TYPE_GENERAL
            )
            startActivityForResult(intent, REQUEST_CODE_VIHICLE_SERTIFICATION)
        })

        // 试卷分析和识别

        // 试卷分析和识别
        findViewById<View>(R.id.example_recognize_button).setOnClickListener(View.OnClickListener {
            if (!checkTokenStatus()) {
                return@OnClickListener
            }
            val intent = Intent(mainActivity(), CameraActivity::class.java)
            intent.putExtra(
                CameraActivity.KEY_OUTPUT_FILE_PATH,
                PathUtils.genericOrcImageFilePath()
            )
            intent.putExtra(
                CameraActivity.KEY_CONTENT_TYPE,
                CameraActivity.CONTENT_TYPE_GENERAL
            )
            startActivityForResult(intent, REQUEST_CODE_EXAMPLE_DOC_REG)
        })

        // 手写文字识别

        // 手写文字识别
        findViewById<View>(R.id.written_text_recognize_button).setOnClickListener(View.OnClickListener {
            if (!checkTokenStatus()) {
                return@OnClickListener
            }
            val intent = Intent(mainActivity(), CameraActivity::class.java)
            intent.putExtra(
                CameraActivity.KEY_OUTPUT_FILE_PATH,
                PathUtils.genericOrcImageFilePath()
            )
            intent.putExtra(
                CameraActivity.KEY_CONTENT_TYPE,
                CameraActivity.CONTENT_TYPE_GENERAL
            )
            startActivityForResult(intent, REQUEST_CODE_WRITTEN_TEXT)
        })

        // 护照识别

        // 护照识别
        findViewById<View>(R.id.pass_port_button).setOnClickListener(View.OnClickListener {
            if (!checkTokenStatus()) {
                return@OnClickListener
            }
            val intent = Intent(mainActivity(), CameraActivity::class.java)
            intent.putExtra(
                CameraActivity.KEY_OUTPUT_FILE_PATH,
                PathUtils.genericOrcImageFilePath()
            )
            intent.putExtra(
                CameraActivity.KEY_CONTENT_TYPE,
                CameraActivity.CONTENT_TYPE_PASSPORT
            )
            startActivityForResult(intent, REQUEST_CODE_PASSPORT)
        })

        // 户口本识别

        // 户口本识别
        findViewById<View>(R.id.hukou_recognize_button).setOnClickListener(View.OnClickListener {
            if (!checkTokenStatus()) {
                return@OnClickListener
            }
            val intent = Intent(mainActivity(), CameraActivity::class.java)
            intent.putExtra(
                CameraActivity.KEY_OUTPUT_FILE_PATH,
                PathUtils.genericOrcImageFilePath()
            )
            intent.putExtra(
                CameraActivity.KEY_CONTENT_TYPE,
                CameraActivity.CONTENT_TYPE_GENERAL
            )
            startActivityForResult(intent, REQUEST_CODE_HUKOU_PAGE)
        })

        // 普通机打发票识别

        // 普通机打发票识别
        findViewById<View>(R.id.common_machine_invoice_button).setOnClickListener(View.OnClickListener {
            if (!checkTokenStatus()) {
                return@OnClickListener
            }
            val intent = Intent(mainActivity(), CameraActivity::class.java)
            intent.putExtra(
                CameraActivity.KEY_OUTPUT_FILE_PATH,
                PathUtils.genericOrcImageFilePath()
            )
            intent.putExtra(
                CameraActivity.KEY_CONTENT_TYPE,
                CameraActivity.CONTENT_TYPE_GENERAL
            )
            startActivityForResult(intent, REQUEST_CODE_NORMAL_MACHINE_INVOICE)
        })

        // 自定义模板

        // 自定义模板
        findViewById<View>(R.id.custom_button).setOnClickListener(View.OnClickListener {
            if (!checkTokenStatus()) {
                return@OnClickListener
            }
            val intent = Intent(mainActivity(), CameraActivity::class.java)
            intent.putExtra(
                CameraActivity.KEY_OUTPUT_FILE_PATH,
                PathUtils.genericOrcImageFilePath()
            )
            intent.putExtra(
                CameraActivity.KEY_CONTENT_TYPE,
                CameraActivity.CONTENT_TYPE_GENERAL
            )
            startActivityForResult(intent, REQUEST_CODE_CUSTOM)
        })

        // 磅单识别

        // 磅单识别
        findViewById<View>(R.id.weight_note_button).setOnClickListener(View.OnClickListener {
            if (!checkTokenStatus()) {
                return@OnClickListener
            }
            val intent = Intent(mainActivity(), CameraActivity::class.java)
            intent.putExtra(
                CameraActivity.KEY_OUTPUT_FILE_PATH,
                PathUtils.genericOrcImageFilePath()
            )
            intent.putExtra(
                CameraActivity.KEY_CONTENT_TYPE,
                CameraActivity.CONTENT_TYPE_GENERAL
            )
            startActivityForResult(intent, REQUEST_CODE_WEIGHT_NOTE)
        })

        // 医疗费用明细识别

        // 医疗费用明细识别
        findViewById<View>(R.id.medical_detail_button).setOnClickListener(View.OnClickListener {
            if (!checkTokenStatus()) {
                return@OnClickListener
            }
            val intent = Intent(mainActivity(), CameraActivity::class.java)
            intent.putExtra(
                CameraActivity.KEY_OUTPUT_FILE_PATH,
                PathUtils.genericOrcImageFilePath()
            )
            intent.putExtra(
                CameraActivity.KEY_CONTENT_TYPE,
                CameraActivity.CONTENT_TYPE_GENERAL
            )
            startActivityForResult(intent, REQUEST_CODE_MEDICAL_DETAIL)
        })

        // 网约车行程单识别

        // 网约车行程单识别
        findViewById<View>(R.id.online_taxi_itinerary_button).setOnClickListener(View.OnClickListener {
            if (!checkTokenStatus()) {
                return@OnClickListener
            }
            val intent = Intent(mainActivity(), CameraActivity::class.java)
            intent.putExtra(
                CameraActivity.KEY_OUTPUT_FILE_PATH,
                PathUtils.genericOrcImageFilePath()
            )
            intent.putExtra(
                CameraActivity.KEY_CONTENT_TYPE,
                CameraActivity.CONTENT_TYPE_GENERAL
            )
            startActivityForResult(intent, REQUEST_CODE_ONLINE_TAXI_ITINERARY)
        })

    }

    override fun onBindViewValue() {

    }

    override fun onFindView(rootView: View?) {

    }

    override fun onLayoutId(): Int {
        return R.layout.fragment_ocr
    }

    override fun onPageExit() {

    }

    private fun checkTokenStatus(): Boolean {
        if (!OcrUtils.isAccess()) {
            "token还未成功获取".longToast()
            return false
        }
        return true
    }
}